package com.wiz.weixin.controllor;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.wiz.weixin.TO.StationTO;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.common.GlobalConstant;
import com.wiz.weixin.common.TencentConstant;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.manager.TencentManager;
import com.wiz.weixin.service.StationService;
import com.wiz.weixin.util.PageView;
import com.wiz.weixin.util.PastUtil;

/**
 * 导航
 * @author xxy
 *
 */
@Controller
@RequestMapping("/navigation")
public class NavigationControllor {
	private Gson gson = new GsonBuilder().disableHtmlEscaping().create();
	@Resource
	private TencentManager tencentManager;
	
	@Resource
	private StationService stationService;
	/**跳转到页面中，得到当前位置*/
	@RequestMapping(value="/toLocation")
	public String toLocation(HttpServletRequest request,HttpServletResponse response,
			HttpSession session, Model model) throws UnsupportedEncodingException {
		// 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
		request.setCharacterEncoding("UTF-8");  
		response.setCharacterEncoding("UTF-8"); 
		String code=request.getParameter("code");//签名串
		String appid =  TencentConstant.WX_APP_ID; //随机字符串
		String ticket = GlobalConstant.tencent_wx_ticket;
		try {
			String openid = tencentManager.getOpenId(code);//ovNRGs8IVk3ctBk5YLcxE45XG50g
			session.setAttribute("openid", openid);
			
			Map<String, String>map = PastUtil.getParam(appid, ticket);
			
			map.put("code", code);
			model.addAttribute("map", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/navigation/station_list";
		
	}
	/**根据当前位置得到油站列表*/
	@RequestMapping(value="/stationList")
	@ResponseBody
	public String stationsByOpenid(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value = "lat", required = true) double lat,//经度
			@RequestParam(value = "lng", required = true) double lng,//纬度
			@RequestParam(value = "pageNo", required = false,defaultValue="1") int pageNo,//当前页
			@RequestParam(value = "pageSize", required = false,defaultValue="10") int pageSize,//每页显示几条记录
			HttpSession session, Model model) throws UnsupportedEncodingException {
		 // 将请求、响应的编码均设置为UTF-8（防止中文乱码）  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8"); 
        PageView pageView = null;
        try {
        	pageView = new PageView(pageSize, pageNo);
        	
        	String openid = (String) session.getAttribute("openid");
    		System.out.println("session---------openid===>"+openid);
        	/**得到当前用户所注册的所有总部*/
			List<String> hqIds =  (List<String>) session.getAttribute("hqIds");
			if(hqIds == null || hqIds.size()<= 0){
				hqIds = stationService.getHQIds(openid);
				session.setAttribute("hqIds", hqIds);
			}
        	
        	/**根据总部列表得到，附近的加油站*/
        	pageView = stationService.stationsByHq(hqIds,lat,lng,pageView);// 30.635549,104.074860,
        	pageView.setCode(200);
		} catch (Exception e) {
			e.printStackTrace();
			pageView.setCode(300);
		}
		return gson.toJson(pageView);
		
	}

}
